/* ========================================================================
 * PROJECT: FTIR Blob Tracker
 * ========================================================================
 * This work is based on the original FTIR Blob Tracker developed by
 *   Woonhyuk Baek
 *   Woontack Woo
 *   U-VR Lab, GIST of Gwangju in Korea.
 * 
 *   http://uvr.gist.ac.kr/
 *
 * Copyright of the derived and new portions of this work
 *     (C) 2009 GIST U-VR Lab.
 *
 * This framework is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This framework is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this framework; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * For further information please contact 
 *   Woonhyuk Baek
 *   <windage@live.com>
 *   GIST U-VR Lab.
 *   Department of Information and Communication
 *   Gwangju Institute of Science and Technology
 *   1, Oryong-dong, Buk-gu, Gwangju
 *   South Korea
 * ========================================================================
 ** @author   Woonhyuk Baek
 * ======================================================================== */

#include "Homography.h"
using namespace windage;

bool Homography::Do()
{
	if(this->imagePointList == NULL || this->objectPointList == NULL)
		return false;

	int n = this->imagePointList->size();
	if(n != this->objectPointList->size())
		return false;
	if(n < 4)
		return false;

	float h[9];
	CvMat _h = cvMat(3, 3, CV_32F, h);

	std::vector<CvPoint2D32f> pt1; pt1.resize(n);
	std::vector<CvPoint2D32f> pt2; pt2.resize(n);
	for(int i=0; i<n; i++)
	{
		windage::Vector2 impt = (*this->imagePointList)[i];
		windage::Vector2 obpt = (*this->objectPointList)[i];
		
		if(isFlip)
		{
			pt1[i] = cvPoint2D32f(impt.x, height - impt.y - 1);
		}
		else
		{
			pt1[i] = cvPoint2D32f(impt.x, impt.y);
		}
		pt2[i] = cvPoint2D32f(obpt.x, obpt.y);
	}

	CvMat _pt1 = cvMat(1, n, CV_32FC2, &(pt1[0]));
	CvMat _pt2 = cvMat(1, n, CV_32FC2, &(pt2[0]));

	cvFindHomography(&_pt1, &_pt2, &_h, CV_LMEDS);

	for(int i=0; i<9; i++)
		this->homography.m1[i] = h[i];
	return true;
}

windage::Vector2 Homography::ConvertObjectToImage(windage::Vector2 point)
{
	windage::Matrix3 inverse = this->homography.Inverse();
	windage::Vector3 tempPt(point.x, point.y, 1.0);
	windage::Vector3 outPt = inverse * tempPt;
	outPt /= outPt.z;

	if(isFlip)
	{
		outPt.y = height - outPt.y - 1;
	}

	return windage::Vector2(outPt.x, outPt.y);
}